import OSS from 'ali-oss';
import {REGION, BUCKET} from '@/constants'
import {getSts} from '@/api/sys'

export const getOssClient = async () => {
  const res = await getSts();
  return new OSS({
    region: REGION,
    accessKeyId: res.Credentials.AccessKeyId,
    accessKeySecret: res.Credentials.AccessKeySecret,
    stsToken: res.Credentials.SecurityToken,
    bucket: BUCKET,

    // 刷新token
    refreshSTSToken: async () => {
      const res = await getSts();
      return {
        accessKeyId: res.Credentials.AccessKeyId,
        accessKeySecret: res.Credentials.AccessKeySecret,
        stsToken: res.Credentials.SecurityToken,
      }
    },

    // 刷新临时凭证的时间间隔
    refreshSTSTokenInterval: 5000,
  })
}

